
#include "ProgressManager.h"
#include "SB.h"
#include <libXml/tree.h>
#include "XMLController.h"
#include "AchievementData.h"

ProgressManager::ProgressManager()
{
    //Init SaveGame

    //Options
    m_sSaveGame.bMusic = true;
    m_sSaveGame.bSfx = true;
    m_sSaveGame.bGameCenter = false;
    m_sSaveGame.bGameCenterAsked = false;
    m_sSaveGame.bAccelerometer = false;
    
    // General stats
    m_sSaveGame.iBabiesFedMax = 0;
    m_sSaveGame.iBabiesFedTotal = 0;
    m_sSaveGame.iCoins = 0;
    m_sSaveGame.iCoinsTotal = 0;
    m_sSaveGame.iCoinsSpent = 0;
    m_sSaveGame.iGamesTotal = 0;
    m_sSaveGame.iPrizesTotal = 0;
    m_sSaveGame.iChainMax = 0;
    
    // Achievements
    for(int i=0; i<MAX_ACHIEVEMENTS; ++i)
    {
        m_sSaveGame.sAchievementData[i].bUnlocked = false;
        m_sSaveGame.sAchievementData[i].iProgress = 0;
    }
    
    // Babies
    for(int i=0; i<MAX_BABY_TYPES; ++i)
    {
        m_sSaveGame.sBabyData[i].bUnlocked = i <= 6;
        m_sSaveGame.sBabyData[i].iPrizesCollected = 0;
    }
    m_sSaveGame.iLastDayPlayed = 0;
//    m_sSaveGame.iBoletsUsed = 0;
/*	
	// Shop items
    for(int i=0; i<6; ++i)
        m_sSaveGame.baUpgrades[i] = 0;
    
    for(int i=0; i<6; ++i)
        m_sSaveGame.iaPowerUps[i] = 0;
 */
}

ProgressManager::~ProgressManager()
{
}

extern "C" const char* getFilePath(const char* fileName);
void ProgressManager::loadAchievements()
{
    xmlDocPtr doc = NULL;
    std::string fileName = "/achievements.xml";
    std::string fullName = getFilePath(fileName.c_str());
    doc = xmlReadFile(fullName.c_str(), "UTF8", 0);
	if (doc == NULL) {
		return;
	}
	xmlNode *root = NULL;
	root = xmlDocGetRootElement(doc);
    
    //Babies
    xmlNode* node = root->children;
    int iCount = 0;
    while(node)
    {
        if(node->type != XML_ELEMENT_NODE)
        {
            node = node->next;
            continue;
        }
        
        AchievementData ad;
        ad.sPacifier = (const char*) xmlGetProp(node, (xmlChar*)"name");
        ad.sPacifier = (const char*) xmlGetProp(node, (xmlChar*)"pacifier");
        ad.iGamerpoints = atoi( (const char*)xmlGetProp(node, (xmlChar*)"gamerpoints"));
        ad.sName = (const char*) xmlGetProp(node, (xmlChar*)"name");
        ad.sDescription = (const char*) xmlGetProp(node, (xmlChar*)"description");
        std::string  strType = (const char*) xmlGetProp(node, (xmlChar*)"accumulate");
        ad.bAccumulate = strcmp(strType.c_str(), "true") == 0;
        ad.iCount = atoi( (const char*)xmlGetProp(node, (xmlChar*)"count"));
        ad.eType = ACH_BABIES_IN_A_GAME;
        m_aAchievements[iCount++] = ad;
        
        node = node->next;
    }
    
	xmlFreeDoc(doc);
}

/*
void ProgressManager::loadUpgrades()
{
    std::string fileName = "/upgrades.xml";
    std::string fullName = getFilePath(fileName.c_str());
    xmlDocPtr doc = xmlReadFile(fullName.c_str(), "UTF8", 0);

	xmlNode *root = xmlDocGetRootElement(doc);
    
    xmlNode* node = root->children;
    int iCount = 0;
    while(node)
    {
        if(node->type != XML_ELEMENT_NODE)
        {
            node = node->next;
            continue;
        }
        
        UpgradeData data;
        data.sName = (const char*) xmlGetProp(node, (xmlChar*)"name");
        data.sIcon = (const char*) xmlGetProp(node, (xmlChar*)"icon");
        data.sCaption = (const char*) xmlGetProp(node, (xmlChar*)"caption");
        data.sDescription = (const char*) xmlGetProp(node, (xmlChar*)"description");
        
        data.bBlocked = xmlHasProp(node, (xmlChar*)"blocked");
        if(data.bBlocked)
        {
            data.sBlocked = (const char*) xmlGetProp(node, (xmlChar*)"blocked");
            data.eBlocked = getUpgradeType(data.sBlocked);
        }
        
        std::string sEnabled = (const char*) xmlGetProp(node, (xmlChar*)"enabled");
        data.bEnabled = strcmp(sEnabled.c_str(), "true") == 0;

        std::string sType = (const char*) xmlGetProp(node, (xmlChar*)"type");
        data.eType = getUpgradeType(sType);
        
        data.iPrize = atoi( (const char*)xmlGetProp(node, (xmlChar*)"price"));

        m_aUpgrades[iCount++] = data;
        
        node = node->next;
    }
    
	xmlFreeDoc(doc);
}

eUpgradeType ProgressManager::getUpgradeType(std::string _sType)
{
    if(_sType == "SuperRainbow")
        return SUPER_RAINBOW;
    else if(_sType == "MegaRainbow")
        return MEGA_RAINBOW;
    else if(_sType == "Vacuum")
        return VACUUM;
    else if(_sType == "Crown")
        return CROWN;
    else if(_sType == "Gloves")
        return GLOVES;
    else
        return ACCELEROMETER;
}

void ProgressManager::substractPowerUp(int _iPowerUp)
{
    SaveGame sg = ProgressManager::instance().getSavedData();
    sg.iaPowerUps[_iPowerUp]--;
    ProgressManager::instance().setSavedData(sg);
}

void ProgressManager::loadPowerups()
{
    std::string fileName = "/powerups.xml";
    std::string fullName = getFilePath(fileName.c_str());
    xmlDocPtr doc = xmlReadFile(fullName.c_str(), "UTF8", 0);
    
	xmlNode *root = xmlDocGetRootElement(doc);
    
    xmlNode* node = root->children;
    int iCount = 0;
    while(node)
    {
        if(node->type != XML_ELEMENT_NODE)
        {
            node = node->next;
            continue;
        }
        
        PowerupData data;
        
        data.sName = (const char*) xmlGetProp(node, (xmlChar*)"name");
        data.sIcon = (const char*) xmlGetProp(node, (xmlChar*)"icon");
        data.sCaption = (const char*) xmlGetProp(node, (xmlChar*)"caption");
        data.sDescription = (const char*) xmlGetProp(node, (xmlChar*)"description");

        std::string sShowCount = (const char*) xmlGetProp(node, (xmlChar*)"showCount");
        data.bShowCount = strcmp(sShowCount.c_str(), "true") == 0;
        
        std::string sEnabled = (const char*) xmlGetProp(node, (xmlChar*)"enabled");
        data.bEnabled = strcmp(sEnabled.c_str(), "true") == 0;
        
        data.iCount = atoi( (const char*)xmlGetProp(node, (xmlChar*)"count"));
        
        std::string sType = (const char*) xmlGetProp(node, (xmlChar*)"type");
        data.eType = getPowerupType(sType);
        
        data.iPrice = atoi( (const char*)xmlGetProp(node, (xmlChar*)"price"));
        
        m_aPowerups[iCount++] = data;
        
        node = node->next;
    }
    
	xmlFreeDoc(doc);
}

ePowerupType ProgressManager::getPowerupType(std::string _sType)
{
    if(_sType == "ExtraRainbow")
        return POWERUP_EXTRA_RAINBOW;
    else if(_sType == "DoubleCoins")
        return POWERUP_DOUBLE_COINS;
    else if(_sType == "BoletTime")
        return POWERUP_BOLET_TIME;
    else if(_sType == "ChainBoost")
        return POWERUP_CHAIN_BOOST;
    else if(_sType == "SuperChainBoost")
        return POWERUP_SUPER_CHAIN_BOOST;
    else
        return POWERUP_PRIZE_SEASON;
}
*/
void ProgressManager::load()
{
    loadAchievements();
    //loadUpgrades();
    //loadPowerups();
    
    // init
}

void ProgressManager::save()
{
    
}

void ProgressManager::updateAchievements()
{
    
}

void ProgressManager::gameCompleted(int _iBabies, int _iCoins, int _iPrizes, int _iChainMax)
{
	m_sSaveGame.iBabiesFedMax = fmax(m_sSaveGame.iBabiesFedMax, _iBabies);
    m_sSaveGame.iBabiesFedTotal += _iBabies;
    m_sSaveGame.iCoins += _iCoins;
    m_sSaveGame.iCoinsTotal += _iCoins;
    m_sSaveGame.iGamesTotal++;
    m_sSaveGame.iPrizesTotal += _iPrizes;
    m_sSaveGame.iChainMax = fmax(m_sSaveGame.iChainMax, _iChainMax);
}

std::string ProgressManager::getAchievementDesc(int _iIndex)
{ 
    if(m_aAchievements[_iIndex].bAccumulate)
    {
        char buff[30];
        sprintf(buff, "%i/%i", m_sSaveGame.sAchievementData[_iIndex].iProgress, m_aAchievements[_iIndex].iCount);
        return m_aAchievements[_iIndex].sDescription + "  (" + buff + ")";
    }
    else
        return m_aAchievements[_iIndex].sDescription;
}
